package com.syni.mdd.sjb.common.datatreaure.aop;
import com.syni.mdd.sjb.common.component.utils.common.annotations.CheckUserIdentity;
import com.syni.mdd.sjb.common.component.utils.common.utils.RequestHeaderData;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.sjb.common.component.utils.common.utils.StringUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.WebUtils;
import com.syni.mdd.sjb.common.component.utils.feign.service.mdd.commonuser.UserMerchantsRelationRestService;
import com.syni.mdd.sjb.common.component.utils.feign.service.synimerchants.SyniMerchantsAccountRestService;
import com.syni.mdd.sjb.common.datatreaure.constants.Constants;
import com.syni.mdd.sjb.common.datatreaure.constants.ResultCode;
import com.syni.mdd.sjb.common.datatreaure.constants.ResultMsg;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.BaseRqVo;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @className CheckParamsAspect
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/25 17:23
 * @VERSION 1.0
 **/
@Aspect
@Order(2)
@Component
public class ControllerCheckUserIdentify {
    private static String TOKEN_CHECK = Constants.TOKEN_CHECK;

    private final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    SyniMerchantsAccountRestService syniMerchantsAccountRestService;

    @Autowired
    UserMerchantsRelationRestService userMerchantsRelationRestService;

    @Around("@annotation(checkUserIdentity)")
    public Object around(ProceedingJoinPoint proceedingJoinPoint,  CheckUserIdentity checkUserIdentity) throws Throwable {
        Map<String,Object> argsMap = new HashMap<>();
        Object[] args = proceedingJoinPoint.getArgs();
        BaseRqVo baseRqVo = null;
        if(args.length > 0){
            for(Object arg : args){
                if(arg instanceof BaseRqVo){
                    baseRqVo = (BaseRqVo) arg;
                }
            }
        }
        RequestHeaderData requestHeaderData = new RequestHeaderData();
        String token = requestHeaderData.getString("token");
        if(StringUtils.isBlank(token)){
            log.info("lose token");
            return ResultMapHelper.result(ResultCode.CODE_USER_TOKEN_FAIL, ResultMsg.MSG_TOKEN_FAIL);
        }
        String USER_TOKEN_CHECK = requestHeaderData.getString("user_token_check");
        String CHECK = WebUtils.MD5(token + TOKEN_CHECK);
        if(!CHECK.equals(USER_TOKEN_CHECK)) {
            log.info("CHECK ERROR");
            return ResultMapHelper.result(ResultCode.CODE_USER_TOKEN_FAIL, ResultMsg.MSG_TOKEN_FAIL);
//            log.info("zuulCheck fail");
//            //校验是否是统一商户系统派发的token
//            RpcResponseVo rpcResponseVo = syniMerchantsAccountRestService.checkMerchantsAccountToken(token);
//            if(!rpcResponseVo.isSuccess()) {
//                return ResultMapHelper.result(ResultCode.CODE_USER_TOKEN_FAIL, ResultMsg.MSG_TOKEN_FAIL);
//            }
//            String merchantsAccountId = rpcResponseVo.getDataElement("merchantsAccountId").toString();
//            rpcResponseVo = userMerchantsRelationRestService.getUserIdByMerchantsAccountId(merchantsAccountId);
//            if(rpcResponseVo.isSuccess()) {
//                Integer userId = Integer.parseInt(rpcResponseVo.getDataElement("userId").toString());
//                if (baseRqVo != null) {
//                    Integer client = requestHeaderData.getInteger("client");
//                    baseRqVo.setUserId(userId);
//                    baseRqVo.setToken(token);
//                    baseRqVo.setClient(client);
//                    baseRqVo.setMerchantsAccountId(merchantsAccountId);
//                    return proceedingJoinPoint.proceed(args);
//                }
//            }
        }
        Integer userId = requestHeaderData.getInteger("userid");
        String merchantsId = requestHeaderData.getString("merchantsid");
        if(userId == null){
            log.info("userId lose");
            return ResultMapHelper.result(ResultCode.CODE_USER_TOKEN_FAIL, ResultMsg.MSG_TOKEN_FAIL);
        }
        if(baseRqVo != null){
            Integer client = requestHeaderData.getInteger("client");
            baseRqVo.setUserId(userId);
            baseRqVo.setToken(token);
            baseRqVo.setClient(client);
        }
        return proceedingJoinPoint.proceed(args);
    }
}
